home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / Lotto / lotto_data_class.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-16  |  9.7 KB  |  501 lines  |  [TEXT/CWIE]

  1.  
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <ctype.h>
  6.  
  7. #include "lotto_data_class.h"
  8.  
  9.  
  10. FILE *fp;
  11.  
  12. lotto_data::lotto_data()
  13. {
  14. }
  15.  
  16. lotto_data::~lotto_data()
  17. {
  18. }
  19.  
  20. bool lotto_data::check_for_file( void )
  21. {
  22.     if( (fp = fopen( "lotto.dat", "rb")) == NULL )
  23.     {
  24.         fclose( fp );
  25.         return false;
  26.     } else {
  27.         fclose( fp );
  28.         return true;
  29.     }
  30. }
  31.  
  32. bool lotto_data::set_struct_file( void )
  33. {
  34.     char temp_buf[5] = {NULL};
  35.     int buf_count = 0;
  36.     int set_count = 0;
  37.     
  38.     record[number_of_records] = new lotto_struct;
  39.     
  40.     for( int index = 0; index < 20; index++ )
  41.     {
  42.         if( isdigit( user_input[index] ) != 0 ) // test if we have a digit 0 - 9
  43.         {
  44.             temp_buf[buf_count] = user_input[index];
  45.             buf_count++; // increment the buffer
  46.         }
  47.         
  48.         switch( user_input[index] )
  49.         {
  50.             case '.': 
  51.             {
  52.                 record[number_of_records]->set[set_count] = ( atoi( temp_buf ) );
  53.                 
  54.                 set_count++;
  55.                 
  56.                 for( int index2 = 0; index2 < 5; index2++ )
  57.                 {
  58.                     temp_buf[index2] = 0;
  59.                 }
  60.                 
  61.                 buf_count = 0;
  62.                 break;
  63.             }
  64.             
  65.             case 'q': case 'Q': // user wants to quit
  66.             {
  67.                 delete( record[number_of_records] );
  68.                 lotto_data::clear_user_input();
  69.                 return false;
  70.             }
  71.             
  72.             default: 
  73.             {    
  74.                 if( set_count == 5 ) // then this is the last number
  75.                 {
  76.                     if( user_input[index] == '.' ) // if it is a dot
  77.                     {
  78.                         index++; // move the index up one
  79.                     }
  80.                     
  81.                     for( int count = 0; count < 5; count++ )
  82.                     {
  83.                         temp_buf[count] = 0; // zero out the buffer
  84.                     }
  85.                     buf_count = 0;
  86.                     
  87.                     while( ((user_input[index]) != 0) && (index < 20) )
  88.                     {
  89.                         temp_buf[buf_count] = user_input[index];
  90.                         index++;
  91.                         buf_count++;
  92.                     }
  93.                     
  94.                     record[number_of_records]->set[set_count] = ( atoi( temp_buf ) );
  95.                     bubble_sort_set( number_of_records );
  96.                     lotto_data::clear_user_input();
  97.                     number_of_records++;
  98.                     return true;
  99.                 } else {
  100.                     break;
  101.                     }
  102.             }
  103.         }
  104.     }
  105.     // if something went wrong...
  106.     delete( record[number_of_records] );
  107.     lotto_data::clear_user_input();
  108.     return false;
  109. }
  110.  
  111. bool lotto_data::save_whole_file( void )
  112. {
  113.     lotto_struct *current = NULL;
  114.     
  115.     if( (fp = fopen( "lotto.dat", "wb+" )) == NULL )
  116.     {
  117.         return false;
  118.     } else {    
  119.         for( int index = 0; index < number_of_records; index++ )
  120.         {    
  121.             current = NULL;
  122.             
  123.             if( index == 0 )
  124.             {
  125.                 current = NULL;
  126.                 record[index]->prev = current;
  127.             } else {
  128.                 current = record[index - 1];
  129.                 record[index]->prev = current;
  130.                 }    
  131.             if( index == number_of_records )
  132.             {
  133.                 current = NULL;
  134.                 record[index]->next = current;
  135.             } else {
  136.                 current = record[index + 1];
  137.                 record[index]->next = current;
  138.                 }
  139.             lotto_data::get_stats( index );
  140.             fwrite( record[index], (sizeof( lotto_struct )), 1, fp );
  141.         }
  142.         
  143.     }
  144.     fclose( fp );
  145.     lotto_data::clear_records();
  146.     number_of_records = 0; // done only AFTER clear_records() !
  147.     return true;
  148. }
  149.         
  150. void lotto_data::simple_save( void )
  151. {
  152.     fp = fopen( "lotto.dat", "wb+" );
  153.     for( int pass = 0; pass < number_of_records; pass++ )
  154.     {
  155.         fwrite( record[pass], (sizeof( lotto_struct )), 1, fp );
  156.     }
  157.     fclose( fp );
  158. }
  159.  
  160. void lotto_data::clear_records( void )
  161. {
  162.     for( int index = 0; index < number_of_records; index++ )
  163.     {
  164.         if( record[number_of_records] )
  165.         {
  166.             delete( record[number_of_records] );
  167.         }
  168.     }
  169. }
  170.  
  171.  
  172. void lotto_data::get_stats( int num )
  173. {
  174.     record[num]->evens = 0;
  175.     record[num]->odds = 0;
  176.     record[num]->reds = 0;
  177.     record[num]->whites = 0;
  178.     record[num]->blues = 0;
  179.     
  180.     for( int index = 0; index < 6; index++ )
  181.     {
  182.         // odds & evens
  183.         if( (record[num]->set[index] % 2) == 0 )
  184.         {
  185.             record[num]->evens += 1;
  186.         } else {
  187.             record[num]->odds += 1;
  188.             }
  189.         
  190.         // reds, whites & blues
  191.         if( (record[num]->set[index] > 0) && (record[num]->set[index] < 18) )
  192.         {
  193.             record[num]->reds += 1;
  194.         }
  195.         if( (record[num]->set[index] > 17) && (record[num]->set[index] < 35) )
  196.         {
  197.             record[num]->whites += 1;
  198.         }
  199.         if( (record[num]->set[index] > 34) && (record[num]->set[index] < 52) )
  200.         {
  201.             record[num]->blues += 1;
  202.         }
  203.     }
  204. }
  205.  
  206.  
  207. // bubble sort for a set
  208. void lotto_data::bubble_sort_set( int rec_num )
  209. {
  210.     int size = 6;
  211.     int hold = 0;
  212.     
  213.     // the actual bubble sort
  214.     for( int pass = 1; pass <= (size - 1); pass++ )
  215.     {
  216.         for( int pass2 = 0; pass2 <= (size - 2); pass2++ )
  217.         {
  218.             if( record[rec_num]->set[pass2] > record[rec_num]->set[pass2 + 1] )
  219.             {
  220.                 hold = record[rec_num]->set[pass2];
  221.                 record[rec_num]->set[pass2] = record[rec_num]->set[pass2 + 1];
  222.                 record[rec_num]->set[pass2 + 1] = hold;
  223.             }
  224.         }
  225.     }
  226. }
  227.  
  228.  
  229. void lotto_data::clear_user_input( void )
  230. {
  231.     for( int index = 0; index < 20; index++ )
  232.     {
  233.         user_input[index] = 0;
  234.     }
  235. }
  236.  
  237. bool lotto_data::read_file( void )
  238. {
  239.     if( (fp = fopen( "lotto.dat", "rb" )) == NULL )
  240.     {
  241.         fclose( fp );
  242.         return false;
  243.     } else {
  244.             number_of_records = 0;
  245.             
  246.             for( int index = 0; index < MAX_STRUCTS; index++ )
  247.             {
  248.                 record[index] = new lotto_struct;
  249.                 record[index]->prev = NULL;
  250.                 record[index]->next = NULL;
  251.                 
  252.                 fread( record[index], sizeof( lotto_struct ), 1, fp );
  253.                 
  254.                 if( fp == NULL)
  255.                 {
  256.                     fclose( fp );
  257.                     return false;
  258.                 }
  259.                 if( record[index]->next != NULL )
  260.                 {
  261.                     number_of_records++;
  262.                 }
  263.                 if( record[index]->next == NULL )
  264.                 {
  265.                     fclose( fp );
  266.                     return true;
  267.                 }
  268.             }
  269.         }
  270.     fclose( fp );
  271.     return false;
  272. }
  273.     
  274.     
  275. bool lotto_data::update_struct_file( void )
  276. {
  277.     char temp_buf[5] = {NULL};
  278.     int buf_count = 0;
  279.     int set_count = 0;
  280.     
  281.     lotto_struct *temp_array[MAX_STRUCTS];
  282.     
  283.     for(int ind = 0; ind < 20; ind++ )
  284.     {
  285.         if( (user_input[ind] == 'q') ||  (user_input[ind] == 'Q') )
  286.         {
  287.             return false;
  288.         }
  289.     }
  290.     
  291.     record[number_of_records + 1] = new lotto_struct;
  292.     temp_array[0] = new lotto_struct;
  293.     for(int pass = 0; pass < number_of_records; pass++ )
  294.     {
  295.         temp_array[pass + 1] = new lotto_struct;
  296.         temp_array[pass + 1] = record[pass];
  297.     }
  298.     
  299.     
  300.     
  301.     for( int index = 0; index < 20; index++ )
  302.     {
  303.         if( isdigit( user_input[index] ) != 0 ) // test if we have a digit 0 - 9
  304.         {
  305.             temp_buf[buf_count] = user_input[index];
  306.             buf_count++; // increment the buffer
  307.         }
  308.         
  309.         switch( user_input[index] )
  310.         {
  311.             case '.': 
  312.             {
  313.                 temp_array[0]->set[set_count] = ( atoi( temp_buf ) );
  314.                 
  315.                 set_count++;
  316.                 
  317.                 for( int index2 = 0; index2 < 5; index2++ )
  318.                 {
  319.                     temp_buf[index2] = 0;
  320.                 }
  321.                 
  322.                 buf_count = 0;
  323.                 break;
  324.             }
  325.             
  326.             case 'q': case 'Q': // user wants to quit
  327.             {
  328.                 for( int index3 = 1; index3 < (number_of_records + 1); index3++ )
  329.                 {
  330.                     record[index3] = temp_array[index];
  331.                 }
  332.                 lotto_data::clear_user_input();
  333.                 return false;
  334.             }
  335.             
  336.             default: 
  337.             {    
  338.                 if( set_count == 5 ) // then this is the last number
  339.                 {
  340.                     if( user_input[index] == '.' ) // if it is a dot
  341.                     {
  342.                         index++; // move the index up one
  343.                     }
  344.                     
  345.                     for( int count = 0; count < 5; count++ )
  346.                     {
  347.                         temp_buf[count] = 0; // zero out the buffer
  348.                     }
  349.                     buf_count = 0;
  350.                     
  351.                     while( ((user_input[index]) != 0) && (index < 20) )
  352.                     {
  353.                         temp_buf[buf_count] = user_input[index];
  354.                         index++;
  355.                         buf_count++;
  356.                     }
  357.                     
  358.                     temp_array[0]->set[set_count] = ( atoi( temp_buf ) );
  359.                     
  360.                     for( int index4 = 0; index4 < (number_of_records + 1); index4++ )
  361.                     {
  362.                         record[index4] = temp_array[index4];
  363.                     }
  364.                     
  365.                     bubble_sort_set( number_of_records );
  366.                     lotto_data::clear_user_input();
  367.                     number_of_records++;
  368.                     return true;
  369.                 } else {
  370.                     break;
  371.                     }
  372.             }
  373.         }
  374.     }
  375.     // if something went wrong...
  376.     delete( record[number_of_records] );
  377.     lotto_data::clear_user_input();
  378.     return false;
  379. }
  380.  
  381. bool lotto_data::edit_struct_file( void )
  382. {
  383.     char temp_buf[5] = {NULL};
  384.     int buf_count = 0;
  385.     int set_count = 0;
  386.     int total = 0;
  387.     
  388.     lotto_struct *temp_array[MAX_STRUCTS];
  389.     
  390.     for(int ind = 0; ind < 20; ind++ )
  391.     {
  392.         if( (user_input[ind] == 'q') ||  (user_input[ind] == 'Q') )
  393.         {
  394.             return false;
  395.         }
  396.     }
  397.     
  398.     record[number_of_records + 1] = new lotto_struct;
  399.     
  400.     
  401.     for(int pass = 0; pass < (number_of_records + 1); pass++ )
  402.     {
  403.         temp_array[pass] = new lotto_struct;
  404.     }
  405.     
  406.     for( int pass2 = 0; pass2 <= (number_of_records + 1); pass2++ )
  407.     {
  408.         if( pass2 == (user_request - 1))
  409.         {
  410.             temp_array[pass2 + 1] = record[pass2];
  411.             pass2++;
  412.         } else {
  413.             temp_array[pass2] = record[pass2];
  414.             }
  415.         
  416.     }
  417.     
  418.     
  419.     for( int index = 0; index < 20; index++ )
  420.     {
  421.         if( isdigit( user_input[index] ) != 0 ) // test if we have a digit 0 - 9
  422.         {
  423.             temp_buf[buf_count] = user_input[index];
  424.             buf_count++; // increment the buffer
  425.         }
  426.         
  427.         switch( user_input[index] )
  428.         {
  429.             case '.': 
  430.             {
  431.                 temp_array[user_request - 1]->set[set_count] = ( atoi( temp_buf ) );
  432.                 
  433.                 set_count++;
  434.                 
  435.                 for( int index2 = 0; index2 < 5; index2++ )
  436.                 {
  437.                     temp_buf[index2] = 0;
  438.                 }
  439.                 
  440.                 buf_count = 0;
  441.                 break;
  442.             }
  443.             
  444.             case 'q': case 'Q': // user wants to quit
  445.             {
  446.                 for( int index3 = 1; index3 < (number_of_records + 1); index3++ )
  447.                 {
  448.                     record[index3] = temp_array[index3];
  449.                 }
  450.                 lotto_data::clear_user_input();
  451.                 return false;
  452.             }
  453.             
  454.             default: 
  455.             {    
  456.                 if( set_count == 5 ) // then this is the last number
  457.                 {
  458.                     if( user_input[index] == '.' ) // if it is a dot
  459.                     {
  460.                         index++; // move the index up one
  461.                     }
  462.                     
  463.                     for( int count = 0; count < 5; count++ )
  464.                     {
  465.                         temp_buf[count] = 0; // zero out the buffer
  466.                     }
  467.                     buf_count = 0;
  468.                     
  469.                     while( ((user_input[index]) != 0) && (index < 20) )
  470.                     {
  471.                         temp_buf[buf_count] = user_input[index];
  472.                         index++;
  473.                         buf_count++;
  474.                     }
  475.                     
  476.                     temp_array[user_request - 1]->set[set_count] = ( atoi( temp_buf ) );
  477.                     
  478.                     total = (number_of_records + 1);
  479.                     number_of_records = 0;
  480.                     
  481.                     for( int index4 = 0; index4 < total; index4++ )
  482.                     {
  483.                         record[index4] = temp_array[index4];
  484.                         number_of_records++;
  485.                     }
  486.                     
  487.                     bubble_sort_set( number_of_records );
  488.                     lotto_data::clear_user_input();
  489.                     return true;
  490.                 } else {
  491.                     break;
  492.                     }
  493.             }
  494.         }
  495.     }
  496.  
  497.     // if something went wrong...
  498.     delete( record[number_of_records] );
  499.     lotto_data::clear_user_input();
  500.     return false;
  501. }